﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using TMDT.BusinessObjects;

namespace TMDT.BusinessLogic
{
    public class HinhAnhManager
    {
        public bool Add(HinhAnh entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spHinhAnhInsert",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDHinhAnh",
                                                                                             entity.IDHinhAnh),
                                                                            new SqlParameter("@IDSanPham",
                                                                                             entity.IDSanPham),
                                                                            new SqlParameter("@URL", entity.URL)
                                                                        }
                                                    });
        }

        public bool Update(HinhAnh entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spHinhAnhUpdate",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDHinhAnh",
                                                                                             entity.IDHinhAnh),
                                                                            new SqlParameter("@IDSanPham",
                                                                                             entity.IDSanPham),
                                                                            new SqlParameter("@URL", entity.URL)
                                                                        }
                                                    });
        }

        public bool Remove(object id)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spHinhAnhDelete",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDSanPham", id)
                                                                        }
                                                    });
        }

        public List<HinhAnh> GetHinhAnh()
        {
            List<HinhAnh> hinhAnh = new List<HinhAnh>();
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText = "select * from HinhAnh",
                                                         CommandType = CommandType.Text,
                                                     });
            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                hinhAnh.Add(HinhAnh.Convert(row));
            }
            return hinhAnh;
        }

        public HinhAnh GetHinhAnh(object id)
        {
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText =
                                                             "select * from HinhAnh where IDHinhAnh = @IDHinhAnh",
                                                         CommandType = CommandType.Text,
                                                         Parameter = new[]
                                                                         {
                                                                             new SqlParameter("@IDHinhAnh", id),
                                                                         }
                                                     });
            return HinhAnh.Convert(dtSet.Tables[0].Rows[0]);
        }

        public HinhAnh GetHinhAnhByIDSanPham(object idSanPham)
        {
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText =
                                                             "select * from HinhAnh where IDSanPham = @IDSanPham",
                                                         CommandType = CommandType.Text,
                                                         Parameter = new[]
                                                                         {
                                                                             new SqlParameter("@IDSanPham", idSanPham),
                                                                         }
                                                     });
            return HinhAnh.Convert(dtSet.Tables[0].Rows[0]);
        }


    }
}
